package com.youkeda.wacai.web.control;


import com.youkeda.wacai.web.model.*;
import com.youkeda.wacai.web.model.service.FinanceService;
import com.youkeda.wacai.web.model.service.RecordService;
import com.youkeda.wacai.web.model.service.impl.JdFinanceServiceImpl;
import com.youkeda.wacai.web.model.service.impl.RecordServiceImpl;
import com.youkeda.wacai.web.model.service.impl.YuebaoFinanceServiceImpl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@RestController
public class AccountingController {
    private static final List<AccountingRecord> records=new ArrayList<>();
    private static final List<Payinfo> payinfos=new ArrayList<>();
    private  static RecordService recordService= new RecordServiceImpl();
//记录前两个月的余额
    @RequestMapping(path="/accounting")
    public Accounting accounting(Accounting accounting){
        double result=accounting.getCash()+accounting.getIncome()-accounting.getRent()-accounting.getCharges()-accounting.getEat();
        accounting.setBalance(result);
        return accounting;
    }
    @RequestMapping(path = "/record")
    public String record(AccountingRecord record){
        if(record.getAmount()==0){
            return " ";
        }
        Date time=new Date();
        String sum ="此次消费记录：";
        record.setTime(time);
        recordService.record(record);
        records.add(record);
        for(AccountingRecord item:records){
           sum +="<br/>"+"记录日期："+item.getTime()+"<br/>"+"消费日期："+item.getCreateTime()+"<br/>"+"余额："+item.getAmount()+"<br/>"+"收支出："
                   +item.getType()+"<br/>"+"生活费用："+item.getCategory()+"<br/>";
        }
        return "记录成功";
    }
    @RequestMapping(path = "/query")
    public List<AccountingRecord> query(){
        return recordService.query();
    }

    @RequestMapping(path="/search")
    public String search(@RequestParam("amount") int amount){
        List<AccountingRecord> fs=records.stream().filter(str->str.getAmount()>amount).collect(Collectors.toList());
        StringBuilder sb=new StringBuilder();
        for(AccountingRecord item:fs){
            sb.append("此次消费记录："+"<br/>"+"记录日期："+item.getTime()+"<br/>");
            sb.append("消费日期："+item.getCreateTime()+"<br/>");
            sb.append("余额："+item.getAmount()+"<br/>");
            sb.append("收支出："+item.getType()+"<br/>");
            sb.append("生活费用："+item.getCategory());
        }
        return sb.toString();
    }
//支付方式选择
    @PostConstruct
    public void init(){
        //初始化白条数据
        Payinfo baitiao=new Payinfo();
        baitiao.setPayType(PayType.baitiao);
        baitiao.setBillingDate(10);
        baitiao.setDueDate(20);
        payinfos.add(baitiao);

        //初始化花呗数据
        Payinfo huabei=new Payinfo();
        huabei.setPayType(PayType.huabei);
        huabei.setBillingDate(12);
        huabei.setDueDate(28);
        payinfos.add(huabei);

        //初始化信用卡数据
        CreditCard creditCard = new CreditCard();
        creditCard.setPayType(PayType.creditCard);
        creditCard.setBillingDate(8);
        creditCard.setDueDate(16);
        creditCard.setName("建设银行：");
        creditCard.setCardNumber("1234321231443");
        payinfos.add(creditCard);
    }

    @RequestMapping(path="/pay")
    public Payinfo pay(@RequestParam("amount") double amount,@RequestParam("payType")PayType payType,@RequestParam("stagesCount")int stageCount){
        List<Payinfo> payinfoList=payinfos.stream().filter(payinfo1 -> payinfo1.getPayType().equals(payType)).collect(Collectors.toList());
        if(payType.equals(PayType.creditCard)){
            CreditCard creditCard=(CreditCard) payinfoList.get(0);
            //返回数据
            CreditCard result=new CreditCard();
            result.setBillingDate(creditCard.getBillingDate());
            result.setDueDate(creditCard.getDueDate());
            result.setPayType(payType);
            result.setAmount(amount);
            result.setStagesCount(stageCount);
            result.setName(creditCard.getName());
            result.setCardNumber(creditCard.getCardNumber());
            return result;
        }
       else{
           Payinfo payinfo=payinfoList.get(0);
           //返回数据
            Payinfo result = new Payinfo();
            result.setBillingDate(payinfo.getBillingDate());
            result.setDueDate(payinfo.getDueDate());
            result.setPayType(payType);
            result.setAmount(amount);
            result.setStagesCount(stageCount);
            return result;
        }
    }

    @RequestMapping(path="/finance")
    public FinanceInfo finance(@RequestParam("type") FinanceType type,@RequestParam("amount")double amount,
                               @RequestParam("days") int days){
        FinanceService financeService=null;
        //根据不同的类型，实例化不同的服务实现者
        if(FinanceType.yuebao.equals(type)){
            financeService= new YuebaoFinanceServiceImpl();
        }else if(FinanceType.jd.equals(type)){
            financeService = new JdFinanceServiceImpl();
        }
        else{
            return null;
        }
        return financeService.invest(amount,days);
    }
}
